package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysStrackMapper;
import com.ruoyi.system.domain.SysStrack;
import com.ruoyi.system.service.ISysStrackService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 追踪记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-10
 */
@Service
public class SysStrackServiceImpl implements ISysStrackService
{
    @Autowired
    private SysStrackMapper sysStrackMapper;

    /**
     * 查询追踪记录
     *
     * @param id 追踪记录主键
     * @return 追踪记录
     */
    @Override
    public SysStrack selectSysStrackById(Long id)
    {
        return sysStrackMapper.selectSysStrackById(id);
    }

    /**
     * 根据客户主键查询追踪记录
     * @param customerId
     * @return
     */
    @Override
    public List<SysStrack> selectSysStrackByCustomerId(Long customerId) {
        return sysStrackMapper.selectSysStrackByCustomerId(customerId);
    }

    /**
     * 查询追踪记录列表
     *
     * @param sysStrack 追踪记录
     * @return 追踪记录
     */
    @Override
    public List<SysStrack> selectSysStrackList(SysStrack sysStrack)
    {
        return sysStrackMapper.selectSysStrackList(sysStrack);
    }

    /**
     * 新增追踪记录
     *
     * @param sysStrack 追踪记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysStrack(SysStrack sysStrack)
    {
        int status = 0;
        try {
            //新增追踪记录判空方法
            emptyInsertSysStrack(sysStrack);
            //创建时间
            sysStrack.setCreateTime(DateUtils.getNowDate());
            //创建人
            sysStrack.setCreateBy(SecurityUtils.getUsername());
            //追踪记录新增方法
            status = sysStrackMapper.insertSysStrack(sysStrack);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        sysStrack.setCreateTime(DateUtils.getNowDate());
        return status;
    }

    /**
     * 新增追踪记录判空方法
     * @param sysStrack
     */
    @Override
    public void emptyInsertSysStrack(SysStrack sysStrack) {
        //追踪方式
        if (null == sysStrack.getTrackMethod()){
            throw new RuntimeException("请选择追踪方式");
        }
        //追踪结果
        if (null == sysStrack.getTrackingResults()){
            throw new RuntimeException("请选择追踪结果");
        }
        //签约可能
        if (null == sysStrack.getSigningResults()){
            throw new RuntimeException("请选择签约可能");
        }
        //电话是否有效
        if (null == sysStrack.getIsValid()){
            throw new RuntimeException("请选择电话是否有效");
        }
        //下次追踪时间
        if (null == sysStrack.getNextTrackTime()){
            throw new RuntimeException("请填写下次追踪日期");
        }
        //线索客户主键
        if (null == sysStrack.getCustomerId()){
            throw new RuntimeException("未传输线索客户主键，请联系管理员");
        }
    }

    /**
     * 修改追踪记录
     *
     * @param sysStrack 追踪记录
     * @return 结果
     */
    @Override
    public int updateSysStrack(SysStrack sysStrack)
    {
        return sysStrackMapper.updateSysStrack(sysStrack);
    }

    /**
     * 批量删除追踪记录
     *
     * @param ids 需要删除的追踪记录主键
     * @return 结果
     */
    @Override
    public int deleteSysStrackByIds(Long[] ids)
    {
        return sysStrackMapper.deleteSysStrackByIds(ids);
    }

    /**
     * 删除追踪记录信息
     *
     * @param id 追踪记录主键
     * @return 结果
     */
    @Override
    public int deleteSysStrackById(Long id)
    {
        return sysStrackMapper.deleteSysStrackById(id);
    }
}